package com.babyqiao.wallpaper.manager.service.impl;

import com.babyqiao.wallpaper.exceptionhandler.WallpaperException;
import com.babyqiao.wallpaper.manager.controller.dto.ActorFullInfo;
import com.babyqiao.wallpaper.manager.controller.dto.ActorQuery;
import com.babyqiao.wallpaper.manager.dao.ActorSocialInfoMapper;
import com.babyqiao.wallpaper.manager.entity.ActorInfo;
import com.babyqiao.wallpaper.manager.dao.ActorInfoMapper;
import com.babyqiao.wallpaper.manager.entity.ActorSocialInfo;
import com.babyqiao.wallpaper.manager.entity.vo.ActorResVO;
import com.babyqiao.wallpaper.manager.service.IActorInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author QLZ
 * @since 2021-02-09
 */
@Service
public class ActorInfoServiceImpl extends ServiceImpl<ActorInfoMapper, ActorInfo> implements IActorInfoService {

    @Autowired
    ActorInfoMapper actorInfoMapper;
    @Autowired
    ActorSocialInfoMapper actorSocialInfoMapper;
    /**
     * 添加演员信息
     *
     * @param actorFullInfo 全部的演员信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setActorService(ActorFullInfo actorFullInfo) {
        ActorInfo actorInfo=new ActorInfo();
        ActorSocialInfo actorSocialInfo=new ActorSocialInfo();
        BeanUtils.copyProperties(actorFullInfo,actorInfo);
        BeanUtils.copyProperties(actorFullInfo,actorSocialInfo);
        int insert = actorInfoMapper.insert(actorInfo);
        System.out.println(insert);
        if(insert!=1){
            throw new WallpaperException(201,"信息添加失败");
        }

        actorSocialInfo.setActorInfoId(actorInfo.getId());

        int insert1 = actorSocialInfoMapper.insert(actorSocialInfo);
        if(insert1!=1){
            throw new WallpaperException(201,"信息添加失败");
        }


        System.out.println(actorInfo);
        System.out.println(actorSocialInfo);

        return true;


    }

    /**
     * 删除演员
     *
     * @param id 演员id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteActorService(String id) {
        QueryWrapper<ActorSocialInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("actor_info_id",id);
        int delete = actorSocialInfoMapper.delete(queryWrapper);
        System.out.println(delete);
        if(delete!=1){
            throw new WallpaperException(201,"删除失败");
        }
        int i = actorInfoMapper.deleteById(id);
        if(i!=1){
            throw new WallpaperException(201,"删除失败");
        }

        return true;
    }

    /**
     * @param actorFullInfo 演员的全部信息
     * @return
     */
    @Override
    public boolean updateActorService(ActorFullInfo actorFullInfo) {
        ActorInfo actorInfo=new ActorInfo();
        ActorSocialInfo actorSocialInfo=new ActorSocialInfo();
        BeanUtils.copyProperties(actorFullInfo,actorInfo);
        BeanUtils.copyProperties(actorFullInfo,actorSocialInfo);
        int i = actorInfoMapper.updateById(actorInfo);
        System.out.println(i);
        if(i!=1){
            throw new WallpaperException(201,"修改失败");
        }

        UpdateWrapper<ActorSocialInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("actor_info_id",actorInfo.getId());
        int update = actorSocialInfoMapper.update(actorSocialInfo, updateWrapper);
        if(update!=1){
            throw new WallpaperException(201,"修改失败");
        }
        return true;
    }

    /**
     * 多条件查询方法
     *
     * @param page       分页
     * @param actorQuery 查询条件
     * @return
     */
    @Override
    public Page<ActorResVO> multipleTablesAndCondition(Page<ActorResVO> page, ActorQuery actorQuery) {
        Page<ActorResVO> andPage = actorInfoMapper.findAndPage(page, actorQuery);
        return andPage;
    }

    /**
     * @param id 演员Id
     * @return
     */
    @Override
    public ActorResVO getActorByIdService(String id) {
        ActorResVO actor = actorInfoMapper.findActorById(id);
        return actor;
    }

    /**
     * 获得全部演员
     *
     * @return ActorResVO 演员信息
     */
    @Override
    public List<ActorResVO> getActorsService() {
        List<ActorResVO> actor =  actorInfoMapper.findActors();
        return actor;
    }
}
